package com.jfinal.ext.ueditor.config;

import com.jfinal.ext.util.RatioUtil;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * @author Timor
 * @version 1.0
 * @date 2023/01/04 20:40:59
 */
public class ImageUploadConfig extends FileUploadConfig {
    public static final Set<Integer> SCALE = Set.of(
            Image.SCALE_DEFAULT,
            Image.SCALE_FAST,
            Image.SCALE_SMOOTH,
            Image.SCALE_REPLICATE,
            Image.SCALE_AREA_AVERAGING);

    private boolean compressEnable = true;
    private int compressBorder = 1920;
    private int compressScale = Image.SCALE_DEFAULT;
    private String insertAlign = null;

    /**
     * 图片是否使用固定比例, 图片高宽比：aspectRatio
     */
    private boolean fixedRatio = false;
    private int optimumWidth = 0;
    private int optimumHeight = 0;
    private List<Integer> compressWidth = Collections.emptyList();
    private List<Integer> compressHeight = Collections.emptyList();

    private String ratio = null;


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public String getRatio() {
        if (null == ratio) {
            ratio = RatioUtil.toRatio(optimumWidth, optimumHeight);
        }
        return ratio;
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    public boolean getCompressEnable() {
        return compressEnable;
    }

    public void setCompressEnable(boolean compressEnable) {
        this.compressEnable = compressEnable;
    }

    public int getCompressBorder() {
        return compressBorder;
    }

    public void setCompressBorder(int compressBorder) {
        this.compressBorder = compressBorder;
    }

    public @NotNull List<Integer> getCompressWidth() {
        return compressWidth;
    }

    public void setCompressWidth(int compressWidth) {
        if (compressWidth > 0) {
            this.compressWidth = List.of(compressWidth);
        }
    }

    public void setCompressWidth(List<Integer> compressWidth) {
        if (null != compressWidth) {
            final List<Integer> items = compressWidth.stream()
                    .filter(item -> null != item && item > 0).toList();

            if (!compressWidth.isEmpty()) {
                this.compressWidth = items;
            }
        }
    }

    public @NotNull List<Integer> getCompressHeight() {
        return compressHeight;
    }

    public void setCompressHeight(int compressHeight) {
        if (compressHeight > 0) {
            this.compressHeight = List.of(compressHeight);
        }
    }

    public void setCompressHeight(List<Integer> compressHeight) {
        if (null != compressHeight) {
            final List<Integer> items = compressHeight.stream()
                    .filter(item -> null != item && item > 0).toList();

            if (!compressHeight.isEmpty()) {
                this.compressHeight = items;
            }
        }
    }

    public int getCompressScale() {
        return compressScale;
    }

    public void setCompressScale(int compressScale) {
        if (SCALE.contains(compressScale)) {
            this.compressScale = compressScale;
        }
    }

    public String getInsertAlign() {
        return insertAlign;
    }

    public void setInsertAlign(String insertAlign) {
        this.insertAlign = insertAlign;
    }


    public boolean getFixedRatio() {
        return fixedRatio;
    }

    public void setFixedRatio(boolean fixedRatio) {
        this.fixedRatio = fixedRatio;
    }

    public int getOptimumWidth() {
        return optimumWidth;
    }

    public void setOptimumWidth(int optimumWidth) {
        this.optimumWidth = optimumWidth;
    }

    public int getOptimumHeight() {
        return optimumHeight;
    }

    public void setOptimumHeight(int optimumHeight) {
        this.optimumHeight = optimumHeight;
    }
}
